package cn.group7.padserver.controller;

import cn.group7.padserver.common.Flag;
import cn.group7.padserver.controller.responseformat.Code;
import cn.group7.padserver.controller.responseformat.Result;
import cn.group7.padserver.pojo.Reserve;
import cn.group7.padserver.service.ReserveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author 谷汉斌
 * @description 预约模块
 * @createTime 2024/8/4 下午2:33
 */
@Api(tags = "预约模块接口")
@RestController
@RequestMapping("/reserves")
public class ReserveController {

    @Resource
    private ReserveService reserveService;

    /**
     * 添加预约
     *
     * @param reserve 预约
     * @return 结果
     */
    @ApiOperation(value = "添加预约情报")
    @PostMapping("/add")
    public Result add(@RequestBody Reserve reserve) {
        boolean add = reserveService.insert(reserve);
        return Result.builder()
                .code(add ? Code.ADD_SUCCESS.getCode() : Code.ADD_FAIL.getCode())
                .message(add ? Code.ADD_SUCCESS.getInfo() : Code.ADD_FAIL.getInfo())
                .flag(add ? Flag.SUCCESS.isCode() : Flag.FAIL.isCode())
                .build();
    }

    /**
     * 根据ID查询预约情况
     *
     * @param id ID
     */
    @ApiOperation(value = "根据ID查询预约情况")
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable("id") Integer id) {
        Reserve reserve = reserveService.selectById(id);
        return Result.builder()
                .code(reserve != null ? Code.QUERY_SUCCESS.getCode() : Code.QUERY_FAIL.getCode())
                .message(reserve != null ? Code.QUERY_SUCCESS.getInfo() : Code.QUERY_FAIL.getInfo())
                .flag(reserve != null ? Flag.SUCCESS.isCode() : Flag.FAIL.isCode())
                .data(reserve)
                .build();
    }

    /**
     * 根据id修改预约情报
     *
     * @param reserve 预约
     */
    @ApiOperation(value = "根据id修改预约情报")
    @PutMapping("/updateById")
    public Result updateById(@RequestBody Reserve reserve) {
        if (null == reserve || reserve.getId() == null) {
            return Result.builder()
                    .code(Code.UPDATE_FAIL.getCode())
                    .message(Code.UPDATE_FAIL.getInfo())
                    .flag(Flag.FAIL.isCode())
                    .build();
        }
        boolean update = reserveService.updateById(reserve);
        return Result.builder()
                .code(update ? Code.UPDATE_SUCCESS.getCode() : Code.UPDATE_FAIL.getCode())
                .message(update ? Code.UPDATE_SUCCESS.getInfo() : Code.UPDATE_FAIL.getInfo())
                .flag(update ? Flag.SUCCESS.isCode() : Flag.FAIL.isCode())
                .build();
    }

    /**
     * 根据id删除预约情报
     *
     * @param id ID
     */
    @ApiOperation(value = "根据id删除预约情报")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        boolean delete = reserveService.deleteById(id);
        return Result.builder()
                .code(delete ? Code.DELETE_SUCCESS.getCode() : Code.DELETE_FAIL.getCode())
                .message(delete ? Code.DELETE_SUCCESS.getInfo() : Code.DELETE_FAIL.getInfo())
                .flag(delete ? Flag.SUCCESS.isCode() : Flag.FAIL.isCode())
                .build();
    }

    /**
     * 根据用户类型查询预约情况
     *
     * @param kind 用户类型
     */
    @ApiOperation(value = "根据用户类型查询预约情况")
    @GetMapping("/selectByKind/{kind}")
    public Result selectByKind(@PathVariable("kind") String kind) {
        return Result.builder()
                .code(Code.QUERY_SUCCESS.getCode())
                .message(Code.QUERY_SUCCESS.getInfo())
                .flag(Flag.SUCCESS.isCode())
                .data(reserveService.selectByKind(kind))
                .build();
    }
}
